<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSettlementsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('settlements', function (Blueprint $table) {
            // 使用雪花ID
            $table->unsignedBigInteger('id')->primary()->comment('结算单ID(雪花ID)');
            $table->string('settlement_number')->unique()->comment('结算单编号');
            $table->string('settlement_name')->comment('结算单名称');
            $table->unsignedBigInteger('tenant_id')->comment('租户ID');
            $table->unsignedBigInteger('task_id')->nullable()->comment('关联任务ID');
            
            // 结算单基本信息
            $table->string('landing_company')->comment('落地公司');
            $table->decimal('total_amount', 15, 2)->default(0)->comment('总金额');
            $table->decimal('total_required_balance', 15, 2)->default(0)->comment('总所需余额');
            $table->decimal('success_amount', 15, 2)->default(0)->comment('结算成功金额');
            $table->unsignedInteger('total_count')->default(0)->comment('总笔数');
            $table->unsignedInteger('success_count')->default(0)->comment('成功笔数');
            
            // 状态枚举：待四要素验证、四要素验证中、四要素验证失败、待提交、待发放、发放中、已完成、已取消、发放失败、部分失败、已退款
            $table->enum('status', ['待四要素验证', '四要素验证中', '四要素验证失败', '待算税', '待提交', '待发放', '发放中', '已完成', '已取消', '发放失败', '部分失败', '已退款'])
                  ->default('待四要素验证')->comment('结算状态');
                  
            // 交付物和备注
            $table->text('deliverables')->nullable()->comment('交付物');
            $table->text('remarks')->nullable()->comment('备注');
            
            // 文件上传相关
            $table->json('uploaded_files')->nullable()->comment('上传的文件信息');
            $table->text('file_note')->nullable()->comment('文件备注');
            
            // 处理时间跟踪
            $table->timestamp('submitted_at')->nullable()->comment('提交时间');
            $table->timestamp('distribute_started_at')->nullable()->comment('开始发放时间');
            $table->timestamp('completed_at')->nullable()->comment('完成时间');
            
            // 操作人员
            $table->unsignedBigInteger('created_by')->nullable()->comment('创建人ID');
            $table->unsignedBigInteger('submitted_by')->nullable()->comment('提交人ID');
            $table->unsignedBigInteger('processed_by')->nullable()->comment('处理人ID');
            
            $table->timestamps();
            $table->softDeletes();
            
            // 索引
            $table->index(['tenant_id', 'status']);
            $table->index(['task_id']);
            $table->index(['settlement_number']);
            $table->index(['landing_company']);
            $table->index(['status']);
            $table->index(['created_at']);
            
            // 外键约束
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->foreign('task_id')->references('id')->on('tasks')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('settlements');
    }
}
